//
//  UIView+Loading.m
//  u9_iOS
//
//  Created by zhangzhenyun on 2017/9/6.
//  Copyright © 2017年 user1. All rights reserved.
//

#import "UIView+Loading.h"
#import <objc/runtime.h>

@implementation UIView (Loading)

- (void)startLoading {
    if (self.loadingView == nil) {
        self.loadingView = [[KPIndicatorView alloc] initWithFrame:CGRectMake(Fix375(8),Fix375(8), Fix375(44), Fix375(44))];
        self.backView = [[UIView alloc] init];
        self.backView.frame = CGRectMake(0, 0, Fix375(60), Fix375(60));
        self.backView.layer.cornerRadius = Fix375(5);
        self.backView.layer.masksToBounds = YES;
        self.backView.alpha = 0.4;
        
        self.backView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 - 49);
        self.backView.backgroundColor = UIColorFromRGB(0x000000);
        [self.backView addSubview:self.loadingView];
    }
    self.backView.hidden = NO;
    [self addSubview:self.backView];
    [self bringSubviewToFront:self.backView];
    [self.loadingView startAnimating];
}
- (void)endLoading {
    self.backView.hidden = YES;
    [self.loadingView stopAnimating];
}
#pragma mark - getter && setter

-(void)setLoadingView:(KPIndicatorView *)loadingView {
    objc_setAssociatedObject(self, @"loadingView", loadingView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(KPIndicatorView *)loadingView {
    return objc_getAssociatedObject(self, @"loadingView");
}
-(UIView *)backView {
    return objc_getAssociatedObject(self, @"backView");
}
-(void)setBackView:(UIView *)backView {
    objc_setAssociatedObject(self, @"backView",backView , OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end
